package com.youxin.cpcenter.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youxin.busfoundation.MessageUtils;
import com.youxin.busfoundation.bean.BusinessMessages;
import com.youxin.busfoundation.bean.ResponseObject;
import com.youxin.busfoundation.bean.login.RequestLoginVerifyCode;
import com.youxin.busfoundation.bean.login.UserInfo;
import com.youxin.busfoundation.bean.sms.RequestSmsBean;
import com.youxin.busfoundation.common.LoginAuth;
import com.youxin.busfoundation.common.VerifyCodeUtils;
import com.youxin.busfoundation.common.redis.RedisOperator;
import com.youxin.busfoundation.sms.SmsUtils;
import com.youxin.busfoundation.utils.security.AesUtils;
import com.youxin.cpcenter.controller.LoginController;
import com.youxin.cpcenter.dao.TbMemberInfoMapper;
import com.youxin.cpcenter.service.SmsService;
import com.youxin.cpcenter.utils.MemberRightsUtils;
import com.youxin.foundation.db.redis.RedisClusterCommand;
import com.youxin.foundation.utils.RandomUtils;
import com.youxin.foundation.utils.RegexUtils;
import com.youxin.foundation.utils.StringUtils;

/**
 * 短信发送模板 您好，{公司}的{职位}{姓名}通过商理事平台向您发起商业合作 合作内容:{内容} 您可以通过:{短信或邮箱}联系到他。
 * 
 * @author yingde.cao
 *
 */
@Service("smsService")
public class SmsServiceImpl extends BaseServiceImpl implements SmsService {
	@Autowired
	private TbMemberInfoMapper tbMemberInfoMapper;
	/**
	 * 短信联系企业联系人
	 */
	@Override
	public ResponseObject sendMessage(RequestSmsBean requestBean) throws Exception {
		MemberRightsUtils.isMemberIdentifyAndPassed(tbMemberInfoMapper);
		UserInfo userInfo=LoginAuth.getUserInfo();
		String mobile = AesUtils.decrypt(requestBean.getMobile(), String.valueOf(userInfo.getId()));
		if(!StringUtils.isNullOrEmpty(mobile))
		{
			requestBean.setMobile(mobile);
		}
		if(!RegexUtils.isMobile(requestBean.getMobile()))
		{
			return ResponseObject.userFailure("手机号码格式错误");
		}
		String content = getContent(requestBean,userInfo);		
		String result = SmsUtils.sendSms(requestBean.getMobile(), content);
		if (SmsUtils.SUCCESS_MESSAGE.equals(result)) {
			return ResponseObject.successObject();
		}
		return ResponseObject.userFailure("短信发送失败");
	}

	/**
	 * 企业试用时注册时的验证码
	 * 
	 * @throws Exception
	 */
	public ResponseObject registerMessage(RequestLoginVerifyCode requestBean) throws Exception {
		if(!RedisOperator.isCanGetTryoutCode(requestBean.getMobile()))
		{
			return new ResponseObject(BusinessMessages.VERIFY_CODE_TIME_FREQUENTLY);
		}
		String verifyCode =VerifyCodeUtils.getVerifyCode();
		String content =MessageUtils.verifyCode(verifyCode);	
		requestBean.setCode(verifyCode);
		String redisResult=RedisOperator.saveTryoutVerifyCode(requestBean);
		if(!RedisClusterCommand.isRedisOk(redisResult))
		{
			logger.error("试用验证码保存到redis错误:"+String.valueOf(redisResult));
			return ResponseObject.userFailure("短信发送失败");
		}
		ResponseObject result = SmsUtils.send(requestBean.getMobile(), content);
		if (result.success()) {
			return ResponseObject.successObject();
		}
		return ResponseObject.userFailure("短信发送失败");
	}

	private String getContent(RequestSmsBean requestBean,UserInfo userInfo) {
		
		String message = "您好，"+userInfo.getCompanyName()+"的"+StringUtils.isNullSetEmpty(userInfo.getPosition())
		+StringUtils.isNullSetEmpty(userInfo.getRealname())+"通过商理事平台向您发起商业合作\n" + "    合作内容:" + requestBean.getMessage() + "\n"
				+ "    您可以通过:"+userInfo.getPhone()+"联系到他。";
		return message;
	}

}
